package com.feshfans.xmpp.client;

import com.feshfans.xmpp.XmppConnectionManager;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * XMPP Client 创建类，所有 Client 都必须通过此类创建
 * 保证所有 Client 在程序运程时只有一个，同一个 Client 类 不能有两个实例对象
 * ToDo 去掉此类
 */
public class ClientFactory {

    // 存储 client,保证返回的 client 只有一份
    private static Map<ClientType,AbstractClient> map = new HashMap<>();

    // 线程安全的方法
    public synchronized static<T extends AbstractClient> T  getClient(ClientType clientType){

        if(map.containsKey(clientType)){
            return (T)map.get(clientType);
        }

        switch (clientType){
            case Account:{
                AccountClient client = new AccountClient(XmppConnectionManager.acquire());
                map.put(clientType,client);
                return (T)client;
            }
            case Roster:{
                RosterClient client = new RosterClient(XmppConnectionManager.acquire());
                map.put(clientType,client);
                return (T)client;
            }
            case Message:{
                MessageClient client = new MessageClient(XmppConnectionManager.acquire());
                map.put(clientType,client);
                return (T)client;
            }
            case Room:{
                RoomClient client = new RoomClient(XmppConnectionManager.acquire());
                map.put(clientType,client);
                return (T) client;
            }
            default:
                throw null;
        }

    }

    public enum ClientType{
        Account,
        Roster,
        Message,
        Room;
    }

}
